﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using UscSunShine.Models;

using PagedList;
using UscSunShine.Mvc.Controllers;

namespace UscSunShine.Mvc.Areas.SuperAdmin.Controllers
{
    /// <summary>
    /// 政策法规管理
    /// </summary>
    [Description("政策法规管理类")]
    public class PolicyManageController : BaseController
    {

        private const int _pageSize = 10;
        // GET: PolicyManage
        [Description("政策法规列表")]
        public ActionResult Index(int? page)
        {
            var posts = _db.Posts.Include(p => p.Department)
                                .Where(t => t.PostCategory == PostCategory.政策法规)
                                .OrderByDescending(t => t.InputDate);
            var pageNumber = page ?? 1;
            return View(posts.ToPagedList(pageNumber, _pageSize));
        }

        // GET: PolicyManage/Details/5
        [Description("政策法规详情")]
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Post post = await _db.Posts.FindAsync(id);
            if (post == null)
            {
                return HttpNotFound();
            }
            return View(post);
        }

        // GET: PolicyManage/Create
        [Description("新建政策法规")]
        public ActionResult Create()
        {
            //ViewBag.DepartmentID = new SelectList(_db.Departments, "ID", "Name");
            var department = _db.Departments.Single(t => t.Name == "党政办");
            var post = new Post
            {
                DepartmentID = department.ID,
                PostCategory = PostCategory.政策法规,
                InputDate = DateTime.Now,
                ViewTimes = 1
            };
            return View(post);
        }

        // POST: PolicyManage/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [Description("新建政策法规，保存")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "ID,DepartmentID,Title,Content,InputDate,ViewTimes,PostCategory")] Post post)
        {
            if (ModelState.IsValid)
            {
                _db.Posts.Add(post);
                await _db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            //ViewBag.DepartmentID = new SelectList(_db.Departments, "ID", "Name", post.DepartmentID);
            return View(post);
        }

        // GET: PolicyManage/Edit/5
        [Description("编辑政策法规")]
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Post post = await _db.Posts.FindAsync(id);
            if (post == null)
            {
                return HttpNotFound();
            }
            //ViewBag.DepartmentID = new SelectList(_db.Departments, "ID", "Name", post.DepartmentID);
            return View(post);
        }

        // POST: PolicyManage/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [Description("编辑政策法规，保存")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "ID,DepartmentID,Title,Content,InputDate,ViewTimes,PostCategory")] Post post)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(post).State = EntityState.Modified;
                await _db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            //ViewBag.DepartmentID = new SelectList(_db.Departments, "ID", "Name", post.DepartmentID);
            return View(post);
        }

        // GET: PolicyManage/Delete/5
        [Description("删除政策法规")]
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Post post = await _db.Posts.FindAsync(id);
            if (post == null)
            {
                return HttpNotFound();
            }
            return View(post);
        }

        // POST: PolicyManage/Delete/5
        [Description("删除政策法规，保存")]
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            Post post = await _db.Posts.FindAsync(id);
            _db.Posts.Remove(post);
            await _db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

    }
}
